#include <controller/member/points_reward_item_controller.h>
#include <service/member/points_reward_item_service.h>

void PointsRewardItemController::registerRoutes(crow::SimpleApp& app)
{
  CROW_ROUTE(app, "/api/points_reward_item/create")
    .methods("POST"_method)([](const crow::request& req) {
      nlohmann::json j;
      auto& body = req.body;
      CHECK_AND_GET_JSON(j);
      CHECK_REQUIRED_FIELDS(j, PointsRewardItemDTO::required_fields);

      auto points_reward_item_dto = PointsRewardItemDTO::from_json(j);

      auto res = PointsRewardItemService::create(points_reward_item_dto);
      return utils::to_response(res, 201);
    });

  CROW_ROUTE(app, "/api/points_reward_item/update")
    .methods("POST"_method)([](const crow::request& req) {
      nlohmann::json j;
      auto& body = req.body;
      CHECK_AND_GET_JSON(j);
      CHECK_REQUIRED_FIELDS(j, PointsRewardItemDTO::required_fields);

      auto item_dto = PointsRewardItemDTO::from_json(j);
      auto item_id = item_dto.item_id;

      auto res = PointsRewardItemService::updateByItemId(item_id, item_dto);
      return utils::to_response(res, 200);
    });

  CROW_ROUTE(app, "/api/points_reward_item/remove")
    .methods("POST"_method)([](const crow::request& req) {
      nlohmann::json j;
      auto& body = req.body;
      CHECK_AND_GET_JSON(j);
      // 检查必填字段
      CHECK_REQUIRED_FIELD(j, "points_reward_item_id");

      auto item_id = j.at("points_reward_item_id").get<ex_id_type>();

      auto res = PointsRewardItemService::removeByItemId(item_id);

      return utils::to_response(res, 200);
    });

  CROW_ROUTE(app, "/api/points_reward_item/getByPage")
    .methods("POST"_method)([](const crow::request& req) {
      nlohmann::json j;
      auto& body = req.body;
      CHECK_AND_GET_JSON(j);

      CHECK_REQUIRED_FIELD(j, "page");
      CHECK_REQUIRED_FIELD(j, "page_size");

      int page = j.value("page", 1);
      int page_size = j.value("page_size", 10);

      auto res = PointsRewardItemService::getByPage(page, page_size);
      return utils::to_response(res, 200);
    });

  CROW_ROUTE(app, "/api/points_reward_item/getAll").methods("GET"_method)([]() {
    auto res = PointsRewardItemService::getAll();
    return utils::to_response(res, 200);
  });

  // 其他路由...
}
